import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login'
import HomePage from '../views/HomePage'
import User from "@/views/backComponents/user/User";
import questionnaire from "@/views/backComponents/questionnaire/questionnaire";
import Graphical from "@/views/backComponents/graphical/graphical";



Vue.use(VueRouter)

const routes = [
  {path:'/homepage',redirect:'/backstage/user'},
  {path: '/login', name: 'Login', component: Login},
  {path:'/homepage' ,component: HomePage},
  {path:'/homepage' ,component: HomePage,
    children:[
      {path: '/backstage/user' ,component: User},
      {path: '/backstage/questionnaire' ,component: questionnaire},
      {path: '/backstage/graphical' ,component: Graphical}
  ]},


]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//挂载路由守卫导航
router.beforeEach((to, from, next) => {
    //to 将要访问的路径
    //from 代表从哪里来
    //next()  放行  next('/login') 强制跳转到login
	if (to.path == '/login') return next();
	//获取token 是否有管理员登录
	const tokenStr = window.sessionStorage.getItem('token')
	if (!tokenStr) return next('/login')
	next()
})

export default router
